package com.cdxy.dmr.repository;

import com.cdxy.dmr.dataObject.IndexLoopDO;
import com.cdxy.dmr.mapper.IndexLoopMapper;
import com.cdxy.model.dto.base.mybatis.BaseRepository;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 首页轮播图 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Repository
@CacheConfig(cacheNames = "index_loop")
public class IndexLoopRepository extends BaseRepository<IndexLoopMapper, IndexLoopDO> {
    /**
     * 获取最近发布的一条
     *
     * @return
     */
    public IndexLoopDO getOrderByCreateTime() {
        return this.baseMapper.selectOne(this.queryWrapper()
                .orderByDesc(IndexLoopDO::getCreateTime)
                .last("LIMIT 1"));
    }

    /**
     * 获取多条
     *
     * @param size
     * @return
     */
    @Cacheable(key = "#size")
    public List<IndexLoopDO> listOrderByCreateTime(int size) {
        return this.baseMapper.selectList(this.queryWrapper()
                .orderByDesc(IndexLoopDO::getCreateTime)
                .last("LIMIT " + size));
    }

}
